package com.lwj.liwanjia.adapter.indent;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.utils.TimeCountDown1;
import com.lwj.liwanjia.view.activity.indent.UnpaidActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/20.
 */

public class UnPaidAdpater extends RecyclerView.Adapter<UnPaidAdpater.MyViewHolder> {
    private Context mContext;
    private List<MyIndentBean.DataBean> list;
    private boolean flag;//用来判断是否是详情 true代表是
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    private OnClickPosition clickPosition;

    public void setClickPosition(OnClickPosition clickPosition) {
        this.clickPosition = clickPosition;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    //用于退出 Activity,避免 Countdown，造成资源浪费。
    private SparseArray<TimeCountDown1> countDownCounters;

    public UnPaidAdpater(Context mContext, List<MyIndentBean.DataBean> list, boolean flag) {
        this.mContext = mContext;
        this.list = list;
        this.flag = flag;
        inflater = LayoutInflater.from(mContext);
        this.countDownCounters = new SparseArray<>();
    }

    /**
     * 清空当前 CountTimeDown 资源
     */
    public void cancelAllTimers() {
        if (countDownCounters == null) {
            return;
        }
        for (int i = 0, length = countDownCounters.size(); i < length; i++) {
            TimeCountDown1 cdt = countDownCounters.get(countDownCounters.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (list.get(viewType).getTitle().equals("购物")) {
            View view = inflater.inflate(R.layout.unpaid_item, parent, false);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.tvState.setText("待付款");
        MyIndentBean.DataBean.GoodsBean goods = list.get(position).getGoods();
        long now = Calendar.getInstance().getTimeInMillis();
        TimeCountDown1 countDown1 = countDownCounters.get(holder.tvSecond.hashCode());
        //去掉复用倒计时
        if (countDown1 != null) {
            countDown1.cancel();
        }
        countDown1 = new TimeCountDown1(goods.getEndPayTime() - now, 1000, holder.tvMinute, holder.tvSecond);
        countDown1.start();
        countDownCounters.put(holder.tvSecond.hashCode(), countDown1);
        if (list.get(position).getTitle().equals("购物")) {
            holder.tvMarket.setText(goods.getShopname());
            holder.tvShangchengNumber.setText("共" + goods.getAmount() + "件商品 合计：");
            holder.tvShangchengPrice.setText("￥" + goods.getTotalpay() + "(含运费￥0.00)");
            Glide.with(mContext).load(goods.getShopimage()).error(R.mipmap.shop_image_2).into(holder.ivMarket);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            holder.rlvGouwuche.setLayoutManager(manager);
            holder.rlvGouwuche.setRefreshEnabled(false);
            holder.rlvGouwuche.setLoadMoreEnabled(false);
            ShopUnpaidAdater1 adapter = new ShopUnpaidAdater1(mContext, goods.getCommodity(), flag);
            holder.rlvGouwuche.setAdapter(adapter);
            adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, Object item, int position1) {
                    if(clickPosition!=null){
                        clickPosition.clickPosition(position);
                    }
                    Intent intent = new Intent(mContext, UnpaidActivity.class);
                    intent.putExtra("tradeId", list.get(position).getGoods().getTradeId());
                    mContext.startActivity(intent);
                }
            });
            if (listener != null) {
                holder.rlGouwu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.OnItemClick(v, position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_gouwu)
        RelativeLayout rlGouwu;
        @BindView(R.id.iv_market)
        ImageView ivMarket;
        @BindView(R.id.tv_market)
        TextView tvMarket;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.rl_market)
        RelativeLayout rlMarket;
        @BindView(R.id.rlv_gouwuche)
        SuperRecyclerView rlvGouwuche;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.tv_shangcheng_price)
        TextView tvShangchengPrice;
        @BindView(R.id.tv_shangcheng_number)
        TextView tvShangchengNumber;
        @BindView(R.id.rl_jiage)
        RelativeLayout rlJiage;
        @BindView(R.id.line1)
        View line1;
        @BindView(R.id.tv_minute)
        TextView tvMinute;
        @BindView(R.id.tv_second)
        TextView tvSecond;
        @BindView(R.id.ll_fukuan)
        LinearLayout llFukuan;
        @BindView(R.id.tv_quxiao_indent)
        TextView tvQuxiaoIndent;
        @BindView(R.id.tv_delete_indent)
        TextView tvDeleteIndent;
        @BindView(R.id.rl_indent_btn)
        RelativeLayout rlIndentBtn;
        @BindView(R.id.indent_btn)
        RelativeLayout indentBtn;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
//public class UnPaidAdpater extends SuperBaseAdapter<String>

//    private List<String> list;//商城
//    private Context mContext;
//    private boolean flag;//用来判断是否是详情 true代表是
//
//    public UnPaidAdpater(Context context, List<String> data, boolean flag) {
//        super(context, data);
//        list = new ArrayList<>();
//        list.add("1");
//        this.flag = flag;
//        this.mContext = context;
//    }
//
//    @Override
//    protected void convert(BaseViewHolder holder, String item, int position) {
//        if (item.equals("0")) {
//            SuperRecyclerView rlvGouwuche = holder.getView(R.id.rlv_gouwuche);
//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            manager.setOrientation(LinearLayoutManager.VERTICAL);
//            rlvGouwuche.setLayoutManager(manager);
//            rlvGouwuche.setRefreshEnabled(false);
//            rlvGouwuche.setLoadMoreEnabled(false);
//            ShopUnpaidAdater adapter = new ShopUnpaidAdater(mContext, list, flag);
//            rlvGouwuche.setAdapter(adapter);
//            adapter.setOnItemClickListener(new OnItemClickListener() {
//                @Override
//                public void onItemClick(View view, Object item, int position) {
//                    Intent i = new Intent(mContext, UnpaidActivity.class);
//                    mContext.startActivity(i);
//                }
//            });
//        } else if (item.equals("5")) {
//            SuperRecyclerView rlvJiudian = holder.getView(R.id.rlv_jiudian);
//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            manager.setOrientation(LinearLayoutManager.VERTICAL);
//            rlvJiudian.setLayoutManager(manager);
//            rlvJiudian.setRefreshEnabled(false);
//            rlvJiudian.setLoadMoreEnabled(false);
//            JiudianAdapter jiudianAdapter = new JiudianAdapter(mContext, list);
//            rlvJiudian.setAdapter(jiudianAdapter);
//            jiudianAdapter.setOnItemClickListener(new OnItemClickListener() {
//                @Override
//                public void onItemClick(View view, Object item, int position) {
//                    Intent i = new Intent(mContext, JiuDianDetailActivity.class);
//                    mContext.startActivity(i);
//                }
//            });
//        } else if (item.equals("4")) {//宾馆
//            holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.binguantouxiang)
//                    .setText(R.id.tv_shangjia_name, "速8宾馆");
//
//        } else if (item.equals("3")) {
//            holder.setImageResource(R.id.iv_shangjia_icon, R.mipmap.touxiang_2)
//                    .setText(R.id.tv_shangjia_name, "KFFFSDDD");
//        } else if (item.equals("6") || item.equals("7") || item.equals("8")) {
//            if (item.equals("6")) {//帮我送
//                holder.setText(R.id.tv_bangwo, "帮我送");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.fahuodizhi)
//                        .setImageResource(R.id.iv_address2, R.mipmap.quhuodizhi);
//            } else if (item.equals("7")) {//帮我取
//                holder.setText(R.id.tv_bangwo, "帮我取");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.quhuodizhi)
//                        .setImageResource(R.id.iv_address2, R.mipmap.shouhuodizhi);
//            } else {
//                holder.setText(R.id.tv_bangwo, "帮我买");
//                holder.setImageResource(R.id.iv_address1, R.mipmap.purchaseadd)
//                        .setImageResource(R.id.iv_address2, R.mipmap.songhuodizhi);
//            }
//        }
//    }
//
//    @Override
//    protected int getItemViewLayoutId(int position, String item) {
//        if (item.equals("0")) {//商城超市
//            return R.layout.unpaid_item;
//        } else if (item.equals("1")) {//寄快递
//            return R.layout.unpaid_kuaidi_item;
//        } else if (item.equals("2")) {//旅游
//            return R.layout.unpaid_lvyou_item;
//        } else if (item.equals("3")) {//搬家
//            return R.layout.unpaid_banjia_item;
//        } else if (item.equals("4")) {//宾馆
//            return R.layout.unpaid_binguan_item;
//        } else if (item.equals("5")) {//酒店和外卖
//            return R.layout.unpaid_jiudian_item;
//        } else if (item.equals("6") || item.equals("7") || item.equals("8")) {//帮我送,帮我买，帮我取
//            return R.layout.unpaid_bangwosong_item;
//        }
//        return 0;
//
//    }
}
